package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author wy
 * @Description mybatisplus自动填充
 * @Date 2023/6/5 17:03
 * @Version 1.0
 */
@Component
public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {

    /**
     * 新增填充
     * @param metaObject  真实的地象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        //获取当前用户
        this.strictInsertFill(metaObject, "createBy", String.class, SecurityUtils.getUsername());

    }

    /**
     * 修改填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        //获取当前用户
        this.strictInsertFill(metaObject, "updateBy", String.class, SecurityUtils.getUsername());

    }
}
